
restore,'./irdc_dist_model/usethese_rb3.sav',/ver
restore,'./local/bgps_hi_spectra.sav',/ver
n = n_elements(rb3)

DIR = './local/ubc_glimpse_proc/'
HIDIR='./local/postage/'

files = FILE_SEARCH(DIR+'*bgps.fits',COUNT=nps)

cnums = strmid(files,23,4)
cnum  = long(cnums)

PDIR = './irdc_dist_model/core_plots/'


chi = '!9' + string("143B) + '!X'
gr_rho = '!9' + string("162B) + '!X'
gr_tau = '!9' + string("164B) + '!X'
delta  = '!9' + string("144B) + '!X'
plusm  = '!9' + string("261B) + '!X'
;;"


;; Get repository params
defsysv, '!MW', exists = exists
IF NOT exists THEN galactic_params 
d = (dindgen(!MW.NBINS)*!MW.BINSIZE + !MW.BINSTART) / 1.d3


set_plot,'ps'
!p.font=0


cgLoadct,3,/silent
plotsym,0,10

FOR i=0L,n-1 DO BEGIN
   
   ;; Make object name (for plot title and filename
   objnam = strtrim(rb3[i].name + '_'  + rb3[i].core, 2)
   
   ;; Find match with BGPS / UBC images
   j = WHERE(cnum EQ rb3[i].cnum)
   
   mydevice,filename=PDIR+objnam+'.eps',ct=3,xsize=13,ysize=6
   
   multiplot,[4,2],gap=0.04,mtitle=objnam+' BGPS #'+cnums[j],mtitsize=0.8,$
             mtitoffset=1,/rowmajor


   ;; First Panel -- BGPS image
   
   bgps = readfits(DIR+cnums[j]+'bgps.fits',bhdr,/silent)
   cgLoadct,3,/silent
   plotimage,bgps,range=set_plot_range(bgps),xst=4,yst=4,/preserve,$
             tit='BGPS',charsize=0.6
   plot_wcs_axes,bhdr,charsize=0.6
   plots,rb3[i].l,rb3[i].b,psym=7,color=cgcolor('Dodger Blue'),symsize=2,$
         thick=4
   
   multiplot
   ;; Second panel -- GLIMPSE image (smoothed to 30")
   
   uns = readfits(DIR+cnums[j]+'smooth30arc.fits',nshdr,/silent)
   pr = set_plot_range(uns)
   cgLoadct,0,/silent
   plotimage,uns,range=pr,xst=4,yst=4,/preserve,$
             tit='GLIMPSE (30" Smooth)',charsize=0.6
   plot_wcs_axes,nshdr,charsize=0.6
   plots,rb3[i].l,rb3[i].b,psym=7,color=cgcolor('Dodger Blue'),symsize=2,$
         thick=4
   
   ;; Add PF09 ellipse, for shits and giggles.
   tvellipse,rb3[i].ir_dy/3600.,rb3[i].ir_dx/3600.,rb3[i].ir_l,rb3[i].ir_b,$
             -rb3[i].ir_pa,/data,thick=1.5,color=cgColor('Deep Pink')
   
      
   multiplot
   ;; Third panel -- GLIMPSE image (smoothed to 11")
   
   smo = readfits(DIR+cnums[j]+'smooth11arc.fits',smhdr,/silent)
   cgLoadct,0,/silent
   plotimage,smo,range=pr,xst=4,yst=4,/preserve,$
             tit='GLIMPSE (11" Smooth)',charsize=0.6
   plot_wcs_axes,smhdr,charsize=0.6
   plots,rb3[i].l,rb3[i].b,psym=7,color=cgcolor('Dodger Blue'),symsize=2,$
         thick=4

    ;; Add PF09 ellipse, for shits and giggles.
   tvellipse,rb3[i].ir_dy/3600.,rb3[i].ir_dx/3600.,rb3[i].ir_l,rb3[i].ir_b,$
             -rb3[i].ir_pa,/data,thick=1.5,color=cgColor('Deep Pink')
  
   
   multiplot
   ;; Fourth panel -- MIR
   
   mir = readfits(DIR+cnums[j]+'Imir.fits',mihdr,/silent)
   cgLoadct,0,/silent
   plotimage,mir,range=pr,xst=4,yst=4,/preserve,$
             tit='I!dMIR!n Estimate',charsize=0.6
   plot_wcs_axes,mihdr,charsize=0.6
   plots,rb3[i].l,rb3[i].b,psym=7,color=cgcolor('Dodger Blue'),symsize=2,$
         thick=4
   
   
   multiplot
   ;; Fifth panel -- HI ON
   hifn = HIDIR+'hi_on'+cnums[j]+'.fits'
   IF FILE_TEST(hifn) THEN BEGIN
      hion = readfits(hifn,hhdr,/silent)
      cgLoadct,3,/silent
      hipr = set_plot_range(hion)
      plotimage,hion,range=hipr,xst=4,yst=4,/preserve,$
                tit='HI "ON" map',charsize=0.6
      plot_wcs_axes,hhdr,charsize=0.6
      plots,rb3[i].l,rb3[i].b,psym=7,color=cgcolor('Dodger Blue'),symsize=2,$
            thick=4
      ;; Add PF09 ellipse, for shits and giggles.
      tvellipse,rb3[i].ir_dy/3600.,rb3[i].ir_dx/3600.,rb3[i].ir_l,rb3[i].ir_b,$
                -rb3[i].ir_pa,/data,thick=1.5,color=cgColor('Deep Pink')
      
   ENDIF
   
   
   multiplot
    ;; Sixth panel -- HI OFF
   hifn = HIDIR+'hi_off'+cnums[j]+'.fits'
   IF FILE_TEST(hifn) THEN BEGIN
      hioff = readfits(hifn,hhdr,/silent)
      cgLoadct,3,/silent
      plotimage,hioff,range=hipr,xst=4,yst=4,/preserve,$
                tit='HI "OFF" map',charsize=0.6
      plot_wcs_axes,hhdr,charsize=0.6
      plots,rb3[i].l,rb3[i].b,psym=7,color=cgcolor('Dodger Blue'),symsize=2,$
            thick=4
      ;; Add PF09 ellipse, for shits and giggles.
      tvellipse,rb3[i].ir_dy/3600.,rb3[i].ir_dx/3600.,rb3[i].ir_l,rb3[i].ir_b,$
                -rb3[i].ir_pa,/data,thick=1.5,color=cgColor('Deep Pink')
      
   ENDIF
   
   multiplot
   ;; Seventh Panel -- HISA image
   hifn = HIDIR+'hisa'+cnums[j]+'.fits'
   IF FILE_TEST(hifn) THEN BEGIN
      hisa = readfits(hifn,hhdr,/silent)
      cgLoadct,3,/silent
      hisapr = set_plot_range(hisa)
      plotimage,hisa,range=hisapr,xst=4,yst=4,/preserve,$
                tit='HISA "ON-OFF" map',charsize=0.6
      plot_wcs_axes,hhdr,charsize=0.6
      plots,rb3[i].l,rb3[i].b,psym=7,color=cgcolor('Dodger Blue'),symsize=2,$
            thick=4
      ;; Add PF09 ellipse, for shits and giggles.
      tvellipse,rb3[i].ir_dy/3600.,rb3[i].ir_dx/3600.,rb3[i].ir_l,rb3[i].ir_b,$
                -rb3[i].ir_pa,/data,thick=1.5,color=cgColor('Deep Pink')
      
   ENDIF
   
   multiplot
   ;; Eighth Panel -- HI on/off spectra, HISA spectrum
   
   hind = WHERE(hi.cnum EQ (cnum[j])[0], nhind)
   hisp = hi[hind]
   
   plot,hisp.v_std,hisp.onspec,charsize=0.6,ytickformat="(I0)",$
        ytit='HI Temperature [K]',xtit='V!dLSR!n [km s!u-1!n]',$
        tit='HI Spectra',yr=[0,180],/yst,/nodata
   oplot,hisp.v_std,hisp.onspec,thick=2,color=cgColor('Red')
   oplot,hisp.v_std,hisp.bdrspec,thick=2,color=cgColor('Charcoal')
   oplot,hisp.v_std,170. - hisp.spectrum,color=cgColor('Forest Green'),thick=2
   
   ;; Line at LSR Velocity in RB3
   vline,rb3[i].vlsr,color=cgColor('Saddle Brown'),thick=2

   ;; Calculate VTAN
   vlsr = vphys2vlsr(rb3[i].l, rb3[i].b, d*1.d3)
   vtan = INTERPOL( vlsr, d*1.d3, !MW.R0*cos(rb3[i].l*!dtor), /LSQUAD)
   vline,vtan,thick=2,color=cgColor('Dodger Blue')
   al_legend,['V!dtan!n'],box=0,textcolor=cgColor('Dodger Blue'),charsize=0.5,$
             position=[vtan,170]
   
   al_legend,['ON','OFF','HISA'],linestyle=0,box=0,charsize=0.6,pos=[100,150],$
             thick=2,color=$
             [cgColor('Red'),cgColor('Charcoal'),cgColor('Forest Green')]
   
   
   multiplot,/reset
   multiplot,/default
   multiplot,/reset
   
   
   ;; GLIMPSE INFORMATION
   
   info = 'I!dmin!n = '+string(rb3[i].i_min,format="(F0.1)")+$
          ' MJy sr!u-1!n    '+$
          'I!dMIR!n = '+string(rb3[i].i_mir,format="(F0.1)")+$
          ' MJy sr!u-1!n    '+$
          delta+'I!dMIR!n = '+string(rb3[i].del_imir,format="(F0.2)")+$
          '    C = '+string(rb3[i].c_meas,format="(F0.2)")+plusm

   sig_c = (rb3[i].i_min/rb3[i].i_mir)*sqrt(rb3[i].del_imir*rb3[i].del_imir + $
                                           0.3*0.3/rb3[i].i_min/rb3[i].i_min)
   info = info + string(sig_c,format="(F0.2)")
   
   al_legend,/normal,position=[0.12,0.05],box=0,info,$
             charsize=0.8
   
   
   
   
   ;; GLIMPSE colorbar
   cgLoadct,0,/silent
   fsc_colorbar, /vert, /right, charsize=0.6, range = pr, $
                 position=[0.49,0.15,0.50,0.90],format="(F0.1)",$
                 title='GLIMPSE Intensity [MJy sr!u-1!n]'

   ;; HI colorbar
   cgLoadct,3,/silent
   fsc_colorbar, /vert, /right, charsize=0.6, range = hipr, $
                 position=[0.72,0.15,0.73,0.90],format="(F0.1)",$
                 title='HI Temperature [K]'

   ;; HISA colorbar
   cgLoadct,3,/silent
   fsc_colorbar, /vert, /right, charsize=0.6, range = hisapr, $
                 position=[0.95,0.58,0.96,0.90],format="(F0.1)",$
                 title='HISA Temperature [K]'





   device,/close_file
   
ENDFOR

set_plot,'x'
!p.font=-1

END
